package com.kkb.controller;

import com.kkb.bean.*;
import com.kkb.mvc.ResponseBody;
import com.kkb.service.CourierService;
import com.kkb.service.ExpressService;
import com.kkb.utils.DateFormatUtil;
import com.kkb.utils.JSONUtil;
import com.kkb.utils.UserUtil;
import org.jacoco.agent.rt.internal_43f5073.core.internal.flow.IFrame;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Stream;

public class CourierController {

    @ResponseBody("/courier/list.do")
    public String findList(HttpServletRequest request, HttpServletResponse response){
        int offset = Integer.parseInt(request.getParameter("offset"));
        int pageNumber = Integer.parseInt(request.getParameter("pageNumber"));
        List<Courier> list = CourierService.findAll(true, offset, pageNumber);
        // 数据转换
        List<TableCourier> newList = new ArrayList<>();
        for (Courier courier : list) {
            String createTime = DateFormatUtil.format(courier.getCreateTime());
            String loginTime = courier.getLoginTime() == null ? "未登录":DateFormatUtil.format(courier.getLoginTime());
            TableCourier tableCourier = new TableCourier(courier.getId(), courier.getcName(), courier.getcPhone(), "********", courier.getcPassword(), courier.getcCount(), createTime, loginTime);
            newList.add(tableCourier);
        }
        // 获取 数据的数量
        Map<String, Integer> console = CourierService.console();
        Result<TableCourier> result = new Result<>(newList,console.get("data1"));
        return JSONUtil.toJSON(result);
    }

    @ResponseBody("/courier/insert.do")
    public String insert(HttpServletRequest request, HttpServletResponse response){
        String cName = request.getParameter("cName");
        String cPhone = request.getParameter("cPhone");
        String cCard = request.getParameter("cCard");
        String cPassword = request.getParameter("cPassword");
        boolean insert = CourierService.insert(new Courier(cName, cPhone, cCard, cPassword));
        Message e = null;
        if (insert) {
            e = new Message(0,"录入成功");
        } else{
            e = new Message(-1,"录入失败");
        }
        return JSONUtil.toJSON(e);
    }
    @ResponseBody("/courier/update.do")
    public String update(HttpServletRequest request, HttpServletResponse response){
        String cName = request.getParameter("cName");
        String cPhone = request.getParameter("cPhone");
        String cCard = request.getParameter("cCard");
        String cPassword = request.getParameter("cPassword");
        int id = Integer.parseInt(request.getParameter("id"));
        boolean insert = CourierService.update(id,new Courier(cName, cPhone, cCard, cPassword));
        Message e = null;
        if (insert) {
            e = new Message(0,"修改成功");
        } else{
            e = new Message(-1,"修改失败");
        }
        return JSONUtil.toJSON(e);
    }

    @ResponseBody("/courier/delete.do")
    public String delete(HttpServletRequest request, HttpServletResponse response){
        int id = Integer.parseInt(request.getParameter("id"));
        boolean delete = CourierService.delete(id);
        Message e = null;
        if (delete) {
            e = new Message(0,"删除成功");
        } else{
            e = new Message(-1,"删除失败");
        }
        return JSONUtil.toJSON(e);
    }

    @ResponseBody("/courier/find.do")
    public String find(HttpServletRequest request, HttpServletResponse response){
        String phone = request.getParameter("phone");
        Courier courier = CourierService.findByPhone(phone);
        Message e = null;
        if (courier != null) {
            e = new Message(0,"查找成功",courier);
        } else{
            e = new Message(-1,"查找失败");
        }
        return JSONUtil.toJSON(e);
    }

    @ResponseBody("/wx/historyExpress.do")
    public String historyExpress(HttpServletRequest request , HttpServletResponse response){
        User userInfo = UserUtil.getUserInfo(request.getSession());
        List<Express> expresses = ExpressService.findBySysPhone(userInfo.getPhoneNumber());
        // 数据转换
        ArrayList<TableExpress> newAll = new ArrayList<>();
        for (Express e : expresses) {
            String code = e.getCode()== null ? "已取件": e.getCode();
            String outtime = e.getOutTime()== null ? "未出库": DateFormatUtil.format(e.getOutTime());
            String intime = DateFormatUtil.format(e.getInTime());
            String status = e.getStatus() == 1 ? "快递已取件":"快递未取件";
            TableExpress tableExpress = new TableExpress(e.getId(),e.getNumber(),e.getUsername(),e.getUserPhone(),e.getCompany(),code,intime,outtime,status,e.getSysPhone());
            newAll.add(tableExpress);
        }
        Message msg = null;
        msg = new Message(0,"查询成功",newAll);
        return JSONUtil.toJSON(msg);
    }
}
